package com.wzt.lianfirecontrol.bean.recode.user;

import android.content.Context;

import com.mob.pushsdk.MobPush;
import com.wzt.lianfirecontrol.bean.BaseModel;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.utils.JSONUtil;
import com.wzt.lianfirecontrol.utils.PreferencesUtils;
import com.wzt.lianfirecontrol.utils.StringUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class UserInfoModel extends BaseModel {

    private String accountNonExpired;
    private String accountNonLocked;
    private String companyId;
    private String companyName;
    private String createTime;
    private String credentialsNonExpired;
    private String deptId;
    private String deptName;
    private String email;
    private String enabled;
    private String id;
    private String imgUrl;
    private String password;
    private String phone;
    private String postId;
    private String postName;
    private String realName;
    private String remark;
    private String repairCompanyId;
    private String repairCompanyName;
    private List<String> roleAlias;
    private String roleIds;
    private String roleNames;
    private String status;
    private String terminal;
    private String updateTime;
    private String username;

    /**
     * 用户在团队树中层级
     */
    private int treeCount = 1;

    public int getTreeCount() {
        return treeCount;
    }

    public void setTreeCount(int treeCount) {
        this.treeCount = treeCount;
    }

    public String getAccountNonExpired() {
        return accountNonExpired;
    }

    public void setAccountNonExpired(String accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }

    public String getAccountNonLocked() {
        return accountNonLocked;
    }

    public void setAccountNonLocked(String accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }

    public String getCompanyId() {
        return companyId;
    }

    public void setCompanyId(String companyId) {
        this.companyId = companyId;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    public void setCredentialsNonExpired(String credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }

    public String getDeptId() {
        return deptId;
    }

    public void setDeptId(String deptId) {
        this.deptId = deptId;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEnabled() {
        return enabled;
    }

    public void setEnabled(String enabled) {
        this.enabled = enabled;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPostId() {
        return postId;
    }

    public void setPostId(String postId) {
        this.postId = postId;
    }

    public String getPostName() {
        return postName;
    }

    public void setPostName(String postName) {
        this.postName = postName;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getRepairCompanyId() {
        return repairCompanyId;
    }

    public void setRepairCompanyId(String repairCompanyId) {
        this.repairCompanyId = repairCompanyId;
    }

    public String getRepairCompanyName() {
        return repairCompanyName;
    }

    public void setRepairCompanyName(String repairCompanyName) {
        this.repairCompanyName = repairCompanyName;
    }

    public String getRoleIds() {
        return roleIds;
    }

    public void setRoleIds(String roleIds) {
        this.roleIds = roleIds;
    }

    public List<String> getRoleAlias() {
        return roleAlias;
    }

    public void setRoleAlias(List<String> roleAlias) {
        this.roleAlias = roleAlias;
    }

    public String getRoleNames() {
        return roleNames;
    }

    public void setRoleNames(String roleNames) {
        this.roleNames = roleNames;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getTerminal() {
        return terminal;
    }

    public void setTerminal(String terminal) {
        this.terminal = terminal;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public UserInfoModel() {
    }

    /**
     * 获取用户账号
     *
     * @param context
     * @return
     */
    public static String getUserAccount(Context context) {
        return PreferencesUtils.getString(context, ConstData.USERACCOUNT, "");
    }

    /**
     * 设置用户账号
     *
     * @param context
     * @return
     */
    public static boolean setUserAccount(Context context, String userAccount) {
        return PreferencesUtils.putString(context, ConstData.USERACCOUNT, userAccount);
    }

    /**
     * 获取用户密码
     *
     * @param context
     * @return
     */
    public static String getUserPassword(Context context) {
        return PreferencesUtils.getString(context, ConstData.USERPASSWORD, "");
    }

    /**
     * 设置用户密码
     *
     * @param context
     * @return
     */
    public static boolean setUserPassword(Context context, String password) {
        return PreferencesUtils.putString(context, ConstData.USERPASSWORD, password);
    }

    public static String getUserToken(Context context) {
        return PreferencesUtils.getString(context, ConstData.USERTOKEN, "");
    }

    /**
     * @param context
     * @return
     */
    public static boolean setUserToken(Context context, String userToken) {
        return PreferencesUtils.putString(context, ConstData.USERTOKEN, userToken);
    }

    /**
     * 获取用户信息
     *
     * @param context
     * @return
     */
    public static UserInfoModel getUserInfo(Context context) {
        UserInfoModel userInfoModel = null;
        try {
            userInfoModel = JSONUtil.jsonObjectToBean(new JSONObject(PreferencesUtils.getString(context, ConstData.USERINFO, "")), UserInfoModel.class);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return userInfoModel;
    }

    /**
     * 获取公司id
     */
    public static String getCompandId(Context context) {
        return PreferencesUtils.getString(context, ConstData.COMPANYID, "");
    }

    public static String getRepairCompanyId(Context context) {
        return PreferencesUtils.getString(context,ConstData.REPAIRCOMPANYID,"");
    }

    public static void setRepairCompanyId(Context context, String repairCompanyId) {
        PreferencesUtils.putString(context,ConstData.REPAIRCOMPANYID,repairCompanyId);
    }

    /**
     * 设置公司id
     *
     * @param context
     */
    public static void setCompandId(Context context, String companyId) {
        PreferencesUtils.putString(context, ConstData.COMPANYID, companyId);
    }


    /**
     * 获取公司id
     */
    public static String getCompandName(Context context) {
        return PreferencesUtils.getString(context, ConstData.COMPANYNAME, "");
    }

    /**
     * 设置公司id
     *
     * @param context
     */
    public static void setCompandName(Context context, String companyName) {
        PreferencesUtils.putString(context, ConstData.COMPANYNAME, companyName);
    }


    /**
     * 是否为当前公司
     *
     * @param context
     * @return
     */
    public static boolean isSelfCompany(Context context) {
        UserInfoModel userInfoModel = getUserInfo(context);
        return getCompandId(context).equals(userInfoModel.getCompanyId());
    }

    /**
     * 判断用户是否登录
     *
     * @param context
     * @return
     */
    public static boolean isLogin(Context context) {
        return !StringUtils.isEmpty(getUserToken(context));
    }

    /**
     * 设置用户信息
     *
     * @param context
     */
    public static void setUserLoginMessage(Context context) {
        UserInfoModel userInfoModel = getUserInfo(context);
        //清空标签。
        //是否获取成功，会在addPushReceiver()->MobPushReceiver-> onTagsCallback(Context context, String[] tags, int operation, int errorCode)中进行回调返回。
        // 当operation为0时，表示获取标签操作；当operation为1时，表示设置标签操作；当operation为2时，表示删除标签操作。当errorCode为0时，表示操作成功；当errorCode非0时，表示操作失败。
        MobPush.cleanTags();

        if (!StringUtils.isEmpty(userInfoModel.getCompanyId())) {
            String[] tags = new String[]{userInfoModel.getCompanyId()};
            /*
            * 添加标签。标签可以添加多个，每次调用都会在原来的基础上进行追加。

是否获取成功，会在addPushReceiver()->MobPushReceiver-> onTagsCallback(Context context, String[] tags, int operation, int errorCode)中进行回调返回。当operation为0时，表示获取标签操作；
* 当operation为1时，表示设置标签操作；当operation为2时，表示删除标签操作。当errorCode为0时，表示操作成功；当errorCode非0时，表示操作失败。
            *
            * */
            MobPush.addTags(tags);
        }
        /*
         * 删除别名。是否删除成功，会在addPushReceiver()->MobPushReceiver-> onAliasCallback(Context context, String alias, int operation, int errorCode)中进行回调返回。当operation为0时，表示获取别名操作；当operation为1时，表示设置别名操作；
         * 当operation为2时，表示删除别名操作。当errorCode为0时，表示操作成功；当errorCode非0时，表示操作失败。
         *
         * */
        MobPush.deleteAlias();

        if (!StringUtils.isEmpty(userInfoModel.getPhone())) {
            //设置别名，别名是唯一的，多次调用，以最后一次设置为准，会进行覆盖；与注册Id是一一对应
            MobPush.setAlias(userInfoModel.getPhone());
        }
        //设置是否显示角标
        MobPush.setShowBadge(true);
    }

    /**
     * 清除用户信息
     *
     * @param context
     */
    public static void clearUserLoginMessage(Context context) {
        UserInfoModel.setCompandId(context, "");
        UserInfoModel.setCompandName(context, "");
        PreferencesUtils.putString(context, ConstData.USERINFO, "");
        UserInfoModel.setUserToken(context, "");
        MobPush.cleanTags();
        MobPush.deleteAlias();
    }
}
